package com.sx.widget.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.sx.widget.R;

public class ProgressBarTv extends FrameLayout {

    private View view;
    private ProgressBar m_progress_bar;
    private TextView m_progress_tv;
    int width;
    int interval = 0;

    public ProgressBarTv(@NonNull Context context) {
        super(context);
        initView();
    }

    public ProgressBarTv(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public void initView() {
        view = LayoutInflater.from(getContext()).inflate(R.layout.view_progressbar_tv, this);
        m_progress_bar = view.findViewById(R.id.m_progress_bar);
        m_progress_tv = view.findViewById(R.id.m_progress_tv);
        setProgressValue(m_progress_bar.getProgress());
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

    }

    public void setProgressValue(int value) {
        width = m_progress_bar.getWidth();
        interval = width / 100;
        m_progress_bar.setProgress(value);
        m_progress_tv.setText(value + "%");
        int result = 0;
        if (value != 0) {
            result = interval * value;
        }
        m_progress_tv.setX(result);
    }


}
